package cn.ym.mall.dao.impl;

import cn.ym.mall.dao.ActivityDao;
import cn.ym.mall.entity.ActiveJoin;
import cn.ym.mall.entity.ActiveType;
import cn.ym.mall.entity.Activity;
import cn.ym.mall.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 漆家乐
 * 活动实现类
 */
public class ActivityDaoImpl implements ActivityDao {
    QueryRunner queryRunner = new QueryRunner();
    /**
     * 发布活动
     *
     * @param activity
     * @return
     * @throws SQLException
     */
    @Override
    public long add(Activity activity) throws SQLException {
        long num = 0;
        try{
            String sql = "insert into ym_activity(aname,acontent,astartTime,aendTime,actid) value(?,?,?,?,?);";
            num = queryRunner.update(DruidUtils.getConnection(),sql,activity);
        }catch (Exception e){
            e.printStackTrace();
        }
        return num;
    }

    /**
     * 查询所有活动
     *
     * @return
     * @throws SQLException
     */
    @Override
    public List<Activity> getAll() throws SQLException {
        List<Activity> activityList = new ArrayList<Activity>();
        try{
            String sql = "select * from ym_activity;";
            activityList = queryRunner.query(DruidUtils.getConnection(),sql,new BeanListHandler<Activity>(Activity.class));
        }catch (Exception e){
            e.printStackTrace();
        }
        return activityList;
    }

    /**
     * 分页查询所有活动
     *
     * @param pageNo
     * @param pageSize
     * @return
     * @throws SQLException
     */
    @Override
    public List<Activity> getActivityPage(Integer pageNo, Integer pageSize) throws SQLException {
        List<Activity> activeJoinList = new ArrayList<Activity>();
        try{
            String sql = "select * from ym_activity order by aid desc limit ?,?;";
            activeJoinList = queryRunner.query(DruidUtils.getConnection(),sql,new BeanListHandler<Activity>(Activity.class),(pageNo-1)*pageSize,pageSize);
        }catch (Exception e){
            e.printStackTrace();
        }
        return activeJoinList;
    }

    /**
     * 查询未结束的活动
     *
     * @param nowTime
     * @return
     * @throws SQLException
     */
    @Override
    public List<Activity> getActiveTypeNotFinished(LocalDateTime nowTime) throws SQLException {
        List<Activity> activityList = new ArrayList<Activity>();
        try{
            String sql = "select * from ym_activity where aendTime>?;";
            activityList = queryRunner.query(DruidUtils.getConnection(),sql,new BeanListHandler<Activity>(Activity.class),nowTime);
        }catch (Exception e){
            e.printStackTrace();
        }
        return activityList;
    }

    /**
     * 查询结束的活动
     *
     * @param nowTime
     * @return
     * @throws SQLException
     */
    @Override
    public List<Activity> getActiveTypeFinished(LocalDateTime nowTime) throws SQLException {
        List<Activity> activityList = new ArrayList<Activity>();
        try{
            String sql = "select * from ym_activity where aendTime<?;";
            activityList = queryRunner.query(DruidUtils.getConnection(),sql,new BeanListHandler<Activity>(Activity.class),nowTime);
        }catch (Exception e){
            e.printStackTrace();
        }
        return activityList;
    }


    /**
     * 分页查询未结束的活动
     *
     * @param nowTime
     * @param pageNo
     * @param pageSize
     * @return
     * @throws SQLException
     */
    @Override
    public List<Activity> getActiveTypeFinishedPage(LocalDateTime nowTime, Integer pageNo, Integer pageSize) throws SQLException {
        List<Activity> activeJoinList = new ArrayList<Activity>();
        try{
            String sql = "select * from ym_activity where aendTime>? order by astartTime desc limit ?,?;";
            activeJoinList = queryRunner.query(DruidUtils.getConnection(),sql,new BeanListHandler<Activity>(Activity.class),nowTime,(pageNo-1)*pageSize,pageSize);
        }catch (Exception e){
            e.printStackTrace();
        }
        return activeJoinList;
    }

    /**
     * 分页查询结束的活动
     *
     * @param nowTime
     * @param pageNo
     * @param pageSize
     * @return
     * @throws SQLException
     */
    @Override
    public List<Activity> getActiveTypeNotFinishedPage(LocalDateTime nowTime, Integer pageNo, Integer pageSize) throws SQLException {
        List<Activity> activeJoinList = new ArrayList<Activity>();
        try{
            String sql = "select * from ym_activity where aendTime<? order by astartTime desc limit ?,?;";
            activeJoinList = queryRunner.query(DruidUtils.getConnection(),sql,new BeanListHandler<Activity>(Activity.class),nowTime,(pageNo-1)*pageSize,pageSize);
        }catch (Exception e){
            e.printStackTrace();
        }
        return activeJoinList;
    }

    /**
     * 查询新闻活动总数
     *
     * @return
     * @throws SQLException
     */
    @Override
    public long getActivityCount() throws SQLException {
        long count = 0;
        try {
            String sql = "select count(0) as count from ym_activity;";
            count = (queryRunner.query(DruidUtils.getConnection(),sql,new ScalarHandler<Long>()));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }

    /**
     * 查询未结束的活动的总数
     *
     * @param nowTime
     * @return
     * @throws SQLException
     */
    @Override
    public long getActivityNotFinishedCount(LocalDateTime nowTime) throws SQLException {
        long count = 0;
        try {
            String sql = "select count(0) as count from ym_activity where aendTime>?;";
            count = (queryRunner.query(DruidUtils.getConnection(),sql,new ScalarHandler<Long>(),nowTime));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }

    /**
     * 查询结束的活动的总数
     *
     * @param nowTime
     * @return
     * @throws SQLException
     */
    @Override
    public long getActivityFinishedCount(LocalDateTime nowTime) throws SQLException {
        long count = 0;
        try {
            String sql = "select count(0) as count from ym_activity where aendTime<?;";
            count = (queryRunner.query(DruidUtils.getConnection(),sql,new ScalarHandler<Long>(),nowTime));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }
}
